package com.message.configuration;

import com.message.MessageSenderTemplate;
import com.message.client.LocalMessageClient;
import com.message.client.MailMessageClient;
import com.message.client.MessageClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Configuration
@ConditionalOnMissingBean(MessageClient.class)
public class MessageAutoConfiguration {

    @Autowired
    private MessageConfiguration configuration;

    /**
     * 本地存储
     * @return
     */
    @Bean
    public List<LocalMessageClient> localMessageClient() {
        return configuration.getLocal().stream().filter(local -> local.getEnable()).map(local -> {
            LocalMessageClient localMessageClient = new LocalMessageClient();
            localMessageClient.setClient(local.getClient());
            localMessageClient.setLocalProperties(local);
            return localMessageClient;
        }).filter(Objects::nonNull).collect(Collectors.toList());
    }

    /**
     * 本地存储
     * @return
     */
    @Bean
    public List<MailMessageClient> mailMessageClient() {
        return configuration.getMail().stream().filter(mailProperties-> mailProperties.getEnable()).map(mailProperties -> {
            MailMessageClient mailMessageClient = new MailMessageClient();
            mailMessageClient.setClient(mailProperties.getClient());
            mailMessageClient.setMailProperties(mailProperties);
            return mailMessageClient;
        }).filter(Objects::nonNull).collect(Collectors.toList());
    }

    /**
     * 文件存储服务
     */
    @Bean
    public MessageSenderTemplate getMessageSenderTemplate(
                                              List<List<? extends MessageClient>> messageClientList
                                              ) {
        MessageSenderTemplate messageSenderTemplate=new MessageSenderTemplate();
        messageSenderTemplate.setMessageClientList(messageClientList);
        messageSenderTemplate.setMessageConfiguration(configuration);
        return messageSenderTemplate;
    }

}
